package com.market.plus.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.market.plus.controller.mapper.JsonMapping;
import com.market.plus.core.dto.ParticipantDto;
import com.market.plus.core.service.NewsService;
import com.market.plus.core.service.ParticipantService;
import com.market.plus.core.util.MarketUtil;

@Controller
public class MarketController {

   public static final String LOGIN_PAGE = "login";

   public static final String LEFT_MENU = "leftMenu";

   private ParticipantService participantService;

   @Autowired
   public MarketController(
         @Qualifier("participantService") ParticipantService participantService,
         @Qualifier("newsService") NewsService newsService,
         @Qualifier("jsonMapping") JsonMapping jsonMapping) {
      this.participantService = participantService;
   }

   @RequestMapping(value = "/main", method = RequestMethod.GET)
   public String viewMainPage(){
      ParticipantDto participantDto = this.participantService.findLoginUserName(true);
      return "redirect:/auth?view="+participantDto.getId().toString();
   }

   @RequestMapping(value = "/auth", method = RequestMethod.GET)
   public String viewPrivate(@RequestParam("view")Long participantId, ModelMap model) {
      ParticipantDto participantDto;
      if (participantId != null){
         participantDto = this.participantService.findId(participantId);
         MarketUtil.getCustomUserDetails().setCurrentViewUser(participantDto);
      }
      String redirectPage = "redirect:/auth/viewInfo";
      return redirectPage;
   }


/*   @RequestMapping(value = "/j_spring_cas_security_check", method = RequestMethod.GET)
   public String viewMainPageA(ModelMap model) {
      return MAIN_PAGE;
   }*/

   @RequestMapping(value = "/viewSystemLeftMenuEditProfile", method = RequestMethod.GET)
   public String viewSystemLeftMenuEditProfile(ModelMap model) {
      return LEFT_MENU;
   }

   @RequestMapping(value = "/login", method = RequestMethod.GET)
   public String login(ModelMap model) {
      return LOGIN_PAGE;
   }
}
